(function($) {
	$(window).load(function(){
		//页面加载时读取cookie(用户名)
		var
				oHomelogin=$(".home-login"),
				oHomezc=$(".home .home-zc"),
				oOtherzc=$(".childenhtml .home-zc");
		if(getCookie("username")){
			oHomelogin.html("欢迎回来,"+getCookie("username"));
			oHomelogin.attr("href","javascript:;");
			oHomezc.html("退出");
			oHomezc.attr("href","html/login.html");
			oOtherzc.html("退出");
			oOtherzc.attr("href","login.html");
		}

		// 搜索头部
		var
			oLeftBtn=$(".home-left li").eq(1),
			oLeftMenu=$(".promenu"),
			oTopBtn=$(".header-ser"),
			oHidTop=$(".hid-header-wrap"),
			oHidTopBtn=$(".hid-header-wrap a"),
			oHidetit=$(".hid-tet"),
			oHideBack=$(".background007");
		oLeftBtn.hover(function(){
			oLeftMenu.stop(true).show(300);
		},function(){
			oLeftMenu.stop(true).hide(300);
		})

		oTopBtn.click(function(){
			if(oHidTop.css("display")=='none'){
				oHidTop.slideDown();
			}else if(oHidTop.css("display")=='block'){
				oHidTop.slideUp();
			}
		})

		oHidTop.mouseenter(function(){
			oHidTopBtn.stop().fadeIn();
		}).mouseleave(function(){
			oHidTopBtn.stop().fadeOut();
		})

		oHidTopBtn.click(function(){
			oHidTop.slideUp();
			oHideBack.hide();
		})

		oHidetit.blur(function(){
			//oHidetit.val("");

		}).bind("input property",function(){
			var
					oVals=$(this).val;
			if(oHidetit.val()){
				//oHideBack.fadeIn();
				$.ajax({
					type:"GET",
					url:"https://suggest.taobao.com/sug",

					data:{q : oHidetit.val()},

					jsonp: 'callback',

                    jsonpcallback: 'jsonp540',

					dataType:"jsonp",
					success:function(data){
						//console.log(data.result);
						var
							sHtml="";
						for(var i=0, len=data.result.length; i<len; i++){
							sHtml+='<li class="lilian'+i+'">'+data.result[i][0]+'</li>';
						}
						if(sHtml){
							$(".background007").fadeIn();
							$(".hide-come").html(sHtml);
							$(".background007 li").each(function(i){
								$(".background007 li").eq(i).css({top:Math.floor(Math.random()*400),left:Math.floor(Math.random()*900)})
							})

						}else{
							$(".background007").fadeOut();
						}
						
					}

				})
			}else{

				oHideBack.fadeOut();
			}
		})

		//滚动客服
		var
			oRightad=$('.right-kefu .totop');
		oRightad.hide();
		$(window).scroll(function(){
			var
				scrollT=$('body').scrollTop();
			if(scrollT<450){
				oRightad.fadeOut(1000);
			}else{
				oRightad.fadeIn(1000);
			}
			//console.log(scrollT);
		})
		//返回头部
		oRightad.click(function(){
			$('body').animate({scrollTop:0});
		})

		//	轮播图
		var
			oBanImg=$(".wrapper ul li"),
			oBanbtn=$(".banner-page a"),
			oBanpre=$(".ban-pre"),
			oBannext=$(".ban-next"),
			oTimer=null,
			oBanindex= 0,
			oBanwrap=$(".wrapper");
		function Banswitch(Index){
			oBanbtn.removeClass().eq(Index).addClass("pagehover");
			oBanImg.stop(true).animate({opacity:0},500).eq(Index).stop(true).animate({opacity:1},500);
		}
		//上一页
		oBanpre.click(function(){
			oBanindex--;
			if(oBanindex<0){
				oBanindex=oBanImg.length-1;
			}
			Banswitch(oBanindex);
		})
		//下一页
		oBannext.click(function(){
			oBanindex++;
			if(oBanindex>=oBanImg.length){
				oBanindex=0;
			}
			Banswitch(oBanindex);
		})
		//banner按钮点击时
		oBanbtn.click(function(){

			Banswitch($(this).index());
		});

		oTimer=setInterval(function(){
			oBanindex++;
			if(oBanindex>=oBanImg.length){
				oBanindex=0;
			}
			Banswitch(oBanindex);
		},2000);

		//滑过banner图层时阻止队列
		oBanwrap.hover(function(){
			//console.log(11)
			clearInterval(oTimer);
			oBanpre.stop(true).fadeIn();
			oBannext.stop(true).fadeIn();
		},function(){
			oBanpre.stop(true).fadeOut();
			oBannext.stop(true).fadeOut();
			oTimer=setInterval(function(){
				oBanindex++;
				if(oBanindex>=oBanImg.length){
					oBanindex=0;
				}
				Banswitch(oBanindex);
			},2000);
		})



		//首页内容区特效
		var
			oBrow=$('.beauty .row'),
			oHealrow=$('.health .row'),
			oLiferow=$('.life .row'),
			oMovpcol=$('.justmove .col');
		//beauty区域划过
		oBrow.hover(function(){
			$(this).find(".description").addClass("b-hover");
			$(this).find(".arrow-left").addClass("b-arrow-hover");
			$(this).find(".arrow-left").siblings().addClass("img-hover2");
			$(this).find(".arrow-right").addClass("b-arrow-hover1");
			$(this).find(".arrow-right").siblings().addClass("img-hover3");

		},function(){
			$(this).find(".description").removeClass("b-hover");
			$(this).find(".arrow-left").removeClass("b-arrow-hover");
			$(this).find(".arrow-left").siblings().removeClass("img-hover2");
			$(this).find(".arrow-right").removeClass("b-arrow-hover1");
			$(this).find(".arrow-right").siblings().removeClass("img-hover3");
		})
		//health区域划过
		oHealrow.hover(function(){
			$(this).find(".description").addClass("h-hover");
			$(this).find(".arrow-left").addClass("h-arrow-hover");
			$(this).find(".arrow-left").siblings().addClass("img-hover2");
			$(this).find(".arrow-right").addClass("h-arrow-hover1");
			$(this).find(".arrow-right").siblings().addClass("img-hover3");

		},function(){
			$(this).find(".description").removeClass("h-hover");
			$(this).find(".arrow-left").removeClass("h-arrow-hover");
			$(this).find(".arrow-left").siblings().removeClass("img-hover2");
			$(this).find(".arrow-right").removeClass("h-arrow-hover1");
			$(this).find(".arrow-right").siblings().removeClass("img-hover3");
		})
		oLiferow.hover(function(){
			$(this).find(".description").addClass("l-hover");
			$(this).find(".arrow-left").addClass("l-arrow-hover");
			$(this).find(".arrow-left").siblings().addClass("img-hover2");
			$(this).find(".arrow-right").addClass("l-arrow-hover1");
			$(this).find(".arrow-right").siblings().addClass("img-hover3");

		},function(){
			$(this).find(".description").removeClass("l-hover");
			$(this).find(".arrow-left").removeClass("l-arrow-hover");
			$(this).find(".arrow-left").siblings().removeClass("img-hover2");
			$(this).find(".arrow-right").removeClass("l-arrow-hover1");
			$(this).find(".arrow-right").siblings().removeClass("img-hover3");
		})
		//
		oMovpcol.hover(function(){
			//console.log($(this).find(".images"));
			$(this).find(".images").find(".arrow-left").siblings().addClass("img-hover2");
			$(this).find(".images").find(".arrow-right").siblings().addClass("img-hover3");

		},function(){
			$(this).find(".images").find(".arrow-left").siblings().removeClass("img-hover2");
			$(this).find(".images").find(".arrow-right").siblings().removeClass("img-hover3");
		})


		//详情页js
		// 详情页关键字点击隐藏
		var
			oInfomore=$(".nav-more"),
			oInfoclose=$(".nav-close"),
			oInfoBox=$(".keywords-box");
		oInfomore.click(function(){
			oInfomore.hide();
			oInfoclose.show();
			oInfoBox.show();
		})
		oInfoclose.click(function(){
			oInfoBox.hide();
			oInfoclose.hide();
			oInfomore.show();
		})

		//翻页逻辑(应该放在setcookie之前)
		var
				oPagebtn=$(".goods-page a:lt(11)").not(".pre-btn"),
				oPageli=$(".goods-piclist li"),
				oPagespre=$(".pre-btn"),
				oPagesnext=$(".next-btn"),
				oPageindex=0;
		oPagebtn.click(function(){
			//console.log(oPagebtn);
			oPageindex=oPagebtn.index($(this));
			if(oPageindex==0){
				window.location.reload();
			}
			console.log(oPageindex);
			oPagebtn.removeClass().eq(oPageindex).addClass("goods-btn");
			if(oPagebtn.eq(oPageindex).text()-0>1){
				oPagespre.show();
			}else{
				oPagespre.hide();
			}
			datas(oPageindex);
		})
		oPagespre.click(function(){
			oPageindex--;
			if(oPageindex<=0){
				oPageindex=0;
				oPagespre.hide();
			}
			oPagebtn.removeClass().eq(oPageindex).addClass("goods-btn");
			datas(oPageindex)
		})
		oPagesnext.click(function(){
			oPagespre.show();
			oPageindex++;
			oPagebtn.removeClass().eq(oPageindex).addClass("goods-btn");
			datas(oPageindex);
		})
		//ajax请求数据函数
		function datas(Index){
			$.ajax({
				url:"json"+Index+".txt",
				type:"GET",
				dataType:"text",
				success:function(data){
					//console.log(eval(data)[0].src);
					var arr=eval(data);
					for(var i= 0, len=oPageli.length; i<len; i++){
						oPageli.eq(i).find(".list-img").find("img").attr("src",arr[i].src);
						oPageli.eq(i).find("h2").html(arr[i].title)
						oPageli.eq(i).find(".goods-price").find("span").html(arr[i].price+"<a href='javascript:;' class='car-links'></a>");
					}
				}
			})
		}

		//详情页添加购物车
		var
			oCarlink=$(".car-links:lt(2)"),
			oCarother=$(".car-links:gt(1)"),
			oCarnum=$(".header-car .car-num"),
			oSorry=$(".sorry"),
			oGotocar=$(".gotocar"),
			oThergoodsnum=1;
		oCarlink.click(function(){
			oCarnum.html(oCarnum.text()-0+1);
			$(".background0").show();
			oGotocar.show().stop(true).animate({"marginTop":"-100"});
			var
					goodsnames=$(this).parents("li").find("h2").text().replace(/\s/g,""),
					goodsids=$(this).parents("li").find("h2").attr("title"),
					goodsprices=$(this).parent().text().trim(),
					goodspics=$(this).parents("li").find(".list-img").find("img").attr("src");
			if(document.cookie.indexOf("goods-name")!=-1&&getCookie("goods-name")!=goodsnames){//已经存在商品且商品名称
				setCookie("goods-pic1",goodspics)
				setCookie("goods-name1",goodsnames);
				setCookie("goods-price1",goodsprices);
				setCookie("goods-id1",goodsids);
				setCookie("goods-num1",oThergoodsnum);
				oThergoodsnum++;
				console.log(oThergoodsnum);
			}else{
				setCookie("goods-pic",goodspics);
				setCookie("goods-name",goodsnames);
				setCookie("goods-price",goodsprices);
				setCookie("goods-id",goodsids);
			}
				setCookie("goods-num",oCarnum.html());
		})
		oCarother.click(function(){
			$(".background0").show();
			oSorry.show().stop(true).animate({"marginTop":"-100"});
		})
		//遮罩层的隐藏
		$(".tips-close").click(function(){
			$(".background0").hide();
			$(".tips-box").stop(true).animate({"marginTop":"-1000"}).hide();
		})
		$(".sure-btn").click(function(){
			$(".background0").hide();
			$(".tips-box").stop(true).animate({"marginTop":"-1000"}).fadeOut();
		})

		//放大镜切换
		var
			oZoom=$(".zoom img")
			oSmalli=$(".small-pic li");
		oSmalli.click(function(){
			//console.log(oZoom.parent());
			//oZoom.parent().attr("href","../img/big"+($(this).index()+1)+".jpg");
			oZoom.attr("src","../img/small"+($(this).index()+1)+".jpg");
			oSmalli.removeClass().eq($(this).index()).addClass("liactive");

		})
		//划过分享
		var
			oMoreBtn=$(".more-btn"),
			oHideinfo=$(".info-hide");
		oMoreBtn.hover(function(){
			oHideinfo.show();
			//if()
		},function(){
			oHideinfo.fadeOut();
		})


		//详情tab选项卡
		var
			oTaba=$(".sub-naver a"),
			oImgtit=$(".img-cont>div");
		oTaba.hover(function(){
			oTaba.removeClass().eq($(this).index()).addClass("tabactive");
			if($(this).index()>0){

				oImgtit.hide().eq($(this).index()).show();
			}else{
				oImgtit.show();
			}
		})

		//购物车数量
		var
				oCarnum=$(".car-num");
		if(getCookie("goods-num")){
			oCarnum.text(getCookie("goods-num"));
		}else{
			oCarnum.text("0")
		}

		// 商品页js
		// 加减计算
		var
			oDece=$(".decrease"),
			oPlus=$(".increase"),
			oNumtit=$(".num-tit");
		oDece.click(function(){
			var
				vals=$(".num-tit").val();
			if(vals<=0){
				oNumtit.val("0");
			}else{
				oNumtit.val(vals-1);
			}
		})
		oPlus.click(function(){
			var
				vals=$(".num-tit").val();
			oNumtit.val(vals-0+1);
		})
		//点击购物车获取商品信息
		var
			oCarBtn=$(".car-btn");
		oCarBtn.click(function(){
			oCarnum.html(oCarnum.text()-0+parseInt(oNumtit.val()));
			$(".background0").show();
			oGotocar.show().stop(true).animate({"marginTop":"-100"});
			var
				goodspics=$(this).parent().siblings(".de-l").find(".cloudzoom").attr("src");
				goodsnames=$(this).siblings("h3").text().replace(/\s/g,""),
				goodsids=$(this).siblings(".goods-info").find(".fr").text().trim(),
				goodsprices=$(this).siblings(".goods-price").text().trim();
				//console.log(goodsnames==getCookie("goods-name"),document.cookie);
			//console.log(goodspics)
			if(document.cookie.indexOf("goods-name")!=-1&&getCookie("goods-name")!=goodsnames){
				setCookie("goods-name1",goodsnames);
				setCookie("goods-id1",goodsids);
				setCookie("goods-price1",goodsprices);
				setCookie("goods-pic1",goodspics);

			}else{
				setCookie("goods-name",goodsnames);
				setCookie("goods-id",goodsids);
				setCookie("goods-price",goodsprices);
				setCookie("goods-pic",goodspics);
			}
			
			setCookie("goods-num",oCarnum.html())
		})
		//页面加载时如果cookie中存在商品数量信息则读取数量
		if(getCookie("goods-num")){
			oCarnum.html(getCookie("goods-num"));
		}

		//购物车页面的计算
		var
			oCarblank=$(".trline"),
			oCarhiden=$(".cart-items"),
			oCarpic=$(".cart-pict img"),
			oCarname=$(".cart-inner .car-itemlink"),
			oCarid=$(".cart-inner .sk-info"),
			oCarprice=$(".jumei-price"),
			oCardis=$(".newdis"),
			oCarnums=$(".tol-num"),//总数量
			oCaradd=$(".newadd"),
			oCartol=$(".total-prices"),
			oCaroption=$(".cart-option a"),
			oCarclear=$(".clear-car"),
			oCarhidedel=$(".del-box"),
			oCardel=$(".dl-sure"),
			oCarnodel=$(".no-dl"),
			reg1=/[0-9]+/g,
			vPrice=0;
		if(getCookie("goods-name")){//如果存在商品名则开始存储数据
			//if(getCookie("goods-name1")){//判断如果存在其他商品时则另存数据
			//	oCarhiden.parent().append(oCarhiden.clone());
			//	var
			//			oNothercar=oCarhiden.siblings(".cart-items");
			//	//vPrice=reg1.exec(getCookie("goods-price1")).join();
			//	oNothercar.show();
			//	oNothercar.find(".cart-pict img").attr("src",getCookie("goods-pic1"));
			//	oNothercar.find(".car-itemlink").text(getCookie("goods-name1"));
			//	oNothercar.find(".sk-info").text(getCookie("goods-id1"));
			//	oNothercar.find(".jumei-price").text(getCookie("goods-price1"));
			//	//oNothercar.find(".total-prices").text("$"+(reg1.exec(getCookie("goods-price1")).join())*getCookie("goods-num1"));
			//}
			oCarblank.hide();
			oCarhiden.show();
			vPrice=reg1.exec(getCookie("goods-price")).join();
			//console.log(vPrice)
			oCarpic.attr("src",getCookie("goods-pic"));
			oCarname.html(getCookie("goods-name"));
			oCarid.html(getCookie("goods-id"));
			oCarprice.html(getCookie("goods-price"));
			oCarnums.val(getCookie("goods-num"))
			oCarnums.text(getCookie("goods-num"));
			oCartol.html("￥"+vPrice*getCookie("goods-num"));
		}
		//数量加减同步

		//
		var
			vals=oCarnums.val();
		oCardis.click(function(){
			if(vals<=1){
				oCarnums.val("1");
				//oCarnums.text("1");
			}else{
				vals=parseInt(vals)-1;
				oCarnums.val(vals);
				oCarnums.text(vals);
			}
			oCartol.html("￥"+vals*vPrice);
			setCookie("goods-num",oCarnums.val());
		})
		oCaradd.click(function(){
			vals=parseInt(vals)+1;
			oCarnums.val(vals);
			oCarnums.text(vals);
			oCartol.html("￥"+vals*vPrice);
			setCookie("goods-num",oCarnums.val());
		})


		oCarnums.val(getCookie("goods-num"));
		//setCookie("tol-pay",$(".total-price").html());

		//全选清除
		var
				oAllselect=$("#all-selector");
		oAllselect.click(function(){
			if(this.checked){
				$(".car-item :checkbox").attr("checked",true);
			}else{
				console.log(1);
				$(".car-item :checkbox").attr("checked",false);
			}

		})

		//删除商品操作
		oCaroption.click(function(){
			oCarhidedel.show().stop(true).animate({top:"-70",opacity:1})
		})
		oCarnodel.click(function() {
			oCarhidedel.stop(true).animate({top:"-100",opacity:"0"}).hide();
		})
		oCardel.click(function(){
			removeCookie("goods-name");
			removeCookie("goods-pic");
			removeCookie("goods-id");
			removeCookie("goods-num");
			removeCookie("goods-price");
			removeCookie("tol-prices")
			window.location.reload();
		})
		//登录验证
		var
				oPaybtn=$(".pay-btn"),
				oHidelogin=$(".loginbox-wrap"),
				oHideclose=$(".loginbox-top span");
		//点击结算时判断cookie中是否存在有用户,没有就进行登录或者注册
		//存在用户则判断是否存在商品,只有商品时才能加载进入结算页
		oPaybtn.click(function(){
			if(!getCookie("username")){
				$(".background0").show();
				oHidelogin.show().stop(true).animate({"marginTop":-195});
			}else{
				if(getCookie("goods-name")){
					window.location.href="order.html";
				}
			}
		})
		//点击关闭弹框登录页
		oHideclose.click(function(){
			$(".background0").hide();
			oHidelogin.stop(true).animate({"marginTop":-1000}).hide();
		})



		//结算页js
		//获取到cookie值
		var
			oCartpict=$(".car-pict img"),
			oCartitem=$(".car-itemlink"),
			oInfoid=$(".goods-count .sk-info"),
			oCartnums=$(".goods-nums span"),
			oCartprice=$(".goods-totalprice span"),
			oOrder=$(".order-info span i"),
			oSubmit=$(".car-submits span i"),
			oBillls=$("#bills"),
			oOrdersubmit=$(".order-btn");
			oBillshid=$(".bills-hide"),
			oButtoninfo=$(".button-info");
		//vPrice=reg1.exec(getCookie("goods-price")).join();
		oCartpict.attr("src",getCookie("goods-pic"));
		oCartitem.text(getCookie("goods-name"));
		oInfoid.text(getCookie("goods-id"));
		oCartnums.text(getCookie("goods-num"));
		oCartprice.text(vPrice*getCookie("goods-num"));
		oOrder.text("￥"+vPrice*getCookie("goods-num"));
		oSubmit.text("￥"+vPrice*getCookie("goods-num"));
		oBillls.parent().click(function(){
			if(oBillls.attr("checked")!="checked"){
				oBillshid.hide();
			}else{
				oBillshid.show();
			}
		})
		oOrdersubmit.click(function(){
			oButtoninfo.show();
		})


		//添加地址
		var
			oRevive=$(".recivename"),
			oDetail=$(".detail-location"),
			oGlobal=$(".global-code"),
			oNowphone=$(".now-phone"),
			aJaxbtn=$(".ajax_location a"),
			oAdressbtn=$(".address-btn"),
			oMaincon=$(".main-container"),
			oNowadd=$(".cc-nowadd"),
			oNewadd=$(".cc-newadd");
		aJaxbtn.click(function(){
			//console.log($(this).siblings("select")[0]);
			$(this).siblings("select").eq(0).find("option").eq(10).attr("selected",true);
			$(this).siblings("select").eq(1).find("option").eq(1).attr("selected",true);
			$(this).siblings("select").eq(2).show().find("option").eq(4).attr("selected",true);
			oDetail.val("综合楼7楼前台旁");
			oGlobal.val("450000");
		})
		oAdressbtn.click(function(){
			if(oRevive.val()&&oNowphone.val()){
				console.log(1);
				oMaincon.hide();
				oNowadd.show().find("a").html("河南"+" "+"郑州"+" "+"河南教育学院"+" "+"45000"+" "+"(收货人:张明 电话:18737154326/--)")

			}
		})
		oNewadd.find("a").click(function(){
			if(oNowadd.css("display")=="block"){
				oMaincon.show();
			}
		});
	})
})(jQuery)